#include <iostream>
#include <queue>
#include <map>
#include <unordered_map>
#include <vector>
#include <algorithm>
#include <cmath>
#include <string>
#include <cstring>
#include <set>
#include <unordered_set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int INF=0x3f3f3f3f;
const ll N=1e5+10,mod =1000000007;
ll a[N],b[N];
ll c[N];
int main()
{
	int n;
	int lena,lenb;
	cin>>n;
	cin>>lena;
	for(int i=lena-1;i>=0;i--)
	{
		cin>>a[i];
	}
	cin>>lenb;
	for(int i=lenb-1;i>=0;i--)
	{
		cin>>b[i];
	}
	
	for(int i=0;i<max(lena,lenb);i++)
	{
		c[i]=max(a[i]+1,b[i]+1);
		c[i]=max((ll)2,c[i]); 
	}
	ll A=0,B=0;
	for(int i=lenb-1;i>=0;i--)
	{
		B=(B*c[i]+b[i])%mod;
	}
	for(int i=lena-1;i>=0;i--)
	{
		A=(A*c[i]+a[i])%mod;
	}
	cout<<(A-B+mod)%mod;
    

	return 0;
}

